package com.zhao.modleclass.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhao.modleclass.Helper.RecyclerViewAdapterHelper;
import com.zhao.modleclass.R;
import com.zhao.modleclass.activity.Live_Fragment_Activity;
import com.zhao.modleclass.bean.LiveBean;

import java.util.List;

/**
 * Created by 张博文 on 2016/11/26.
 */

public class LiveAdapter extends RecyclerViewAdapterHelper<LiveBean.DataBean> {
    private List<LiveBean.DataBean> mList;
    private LinearLayoutManager layoutManager;
    //声明一个这个接口的变量
    private String[] datas;
    private RecyclerView recyclerView_live;

    public LiveAdapter(Context context, List<LiveBean.DataBean> list,RecyclerView recyclerView_live) {
        super(context, list);
        this.mContext=context;
        this.mList=list;
        this.recyclerView_live=recyclerView_live;
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view=mInflater.inflate(R.layout.item_fragment_live,parent,false);
        layoutManager = new LinearLayoutManager(mContext);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=recyclerView_live.getChildAdapterPosition(v);
                smoothMoveToPosition(position);
                Intent intent=new Intent(mContext, Live_Fragment_Activity.class);
                intent.putExtra("CourseId",mList.get(position).getCourseId());
                mContext.startActivity(intent);
            }
        });
        return new MyViewHolder(view);
    }
    /**
     *滚动方法
     */
    private void smoothMoveToPosition(int position) {
        int firstPosition = layoutManager.findFirstVisibleItemPosition();
        int lastPosition = layoutManager.findLastVisibleItemPosition();
        if(position <= lastPosition){
            int top = recyclerView_live.getChildAt(position - firstPosition).getTop();
            recyclerView_live.smoothScrollBy(0, top);
        }
    }

    @Override
    public void onBindMyViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        //自定义holder相同
        if (holder instanceof MyViewHolder){
            //加载图片
            Glide.with(mContext).load(mList.get(position)
            .getImageUrl()).into(((MyViewHolder)holder).iv_live_imageUrl);
            ((MyViewHolder) holder).tv_live_title.setText(mList.get(position).getTitle());
            ((MyViewHolder) holder).tv_live_techNickName.setText(mList.get(position).getTechNickName()+" / "+mList.get(position).getTechJobName());
            if (mList.get(position).getLiveType() == 4){
                ((MyViewHolder) holder).tv_live_liveTime.setText( "回顾 " + mList.get(position).getLiveTime());
            }else {
                ((MyViewHolder) holder).tv_live_liveTime.setText( "预告" + mList.get(position).getStartTime());
            }
        }
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_live_imageUrl;
        private TextView tv_live_title,tv_live_techNickName,tv_live_liveTime;
        public MyViewHolder(View view) {
            super(view);
            iv_live_imageUrl= (ImageView) view.findViewById(R.id.iv_fragment_live_imageUrl);
            tv_live_liveTime= (TextView) view.findViewById(R.id.tv_fragment_live_liveTime);
            tv_live_title= (TextView) view.findViewById(R.id.tv_fragment_live_title);
            tv_live_techNickName= (TextView) view.findViewById(R.id.tv_fragment_live_techNickName);
        }
    }
}
